extends IEventActuator


class_name MountSetter
"""
为指定的结点挂载子结点
"""


## 需要挂载的对象
@export var finder_or_node: Node = null
## 生成子结点的工厂
@export var factory_or_node: Node = null
## 是否需要使用set_deferred执行操作
@export var is_deferred: bool = false
## 是否需要挂载到finder返回的对象上
@export var is_add_child: bool = true
## 作为子节点挂载后执行的动作
@export var child_event: IEventActuator = null
## 如果父结点为ItemContainer自动使用append方式执行
@export var is_appender: bool = true
## 使用append挂载后执行的动作
@export var append_event: IEventActuator = null


func __check_exports() -> Dictionary:
    return IComponent.merge({
        "finder_or_node": IComponent.find_child.bind(self, func(x): return x is INodeFinder, finder_or_node),
        "factory_or_node": IComponent.find_child.bind(self, func(x): return x is INodeFactory, factory_or_node),
    }, super.__check_exports())

func get_wrapper() -> Node:
    return finder_or_node.find() if finder_or_node is INodeFinder else finder_or_node

func get_mount_node() -> Node:
    return factory_or_node.create() if factory_or_node is INodeFactory else factory_or_node

func execute() -> void:
    var node: Node = get_wrapper()
    if node == null:
        return
    var child: Node = get_mount_node()
    if child == null:
        return
    if is_add_child:
        NodeHelper.add_child(node, child, is_deferred)
        IEventActuator.safe_execute(child_event)
    if is_appender and node is ItemContainer:
        if is_deferred:
            node.call_deferred("append", child)
        else:
            node.append(child)
        IEventActuator.safe_execute(append_event)
